<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserDrawResource;
use App\Models\UserDraw;
use Illuminate\Support\Carbon;
use App\Http\Requests\Api\AddressRequest;
use Illuminate\Http\Request;
use App\Http\Queries\UserDrawQuery;

class UserDrawController extends Controller
{
    public function address($id, AddressRequest $request)
    {
        $row = UserDraw::where('id', $id)->firstOrFail();
        $this->authorize('own', $row);

        $row->address = $request->all();
        $row->address_at = Carbon::now();
        $row->save();
        return $this->success(new UserDrawResource($row));
    }

    public function index(Request $request,UserDrawQuery $query){
        $user = $request->user();
        $rows = $query->where('user_id',$user->id)->paginate(10);
        return $this->resource(UserDrawResource::collection($rows));
    }
}
